/**
 * Created by admin on 2017/10/26.
 */
$(document).ready(function() {
    var compare = function (obj1, obj2) {
        var val1 = obj1.ope_id;
        var val2 = obj2.ope_id;
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }
    };
    var VAL = {
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FBPBISDAT  : "FBPBISDAT",
        T_FBPBISOPE	 : "FBPBISOPE",
        T_FBPBISTOL  : "FBPBISTOL",
        T_MSPLINEWO  : "MSPLINEWO",
        T_MTPTOOLMU  : "MTPTOOLMU",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }

    };
    var dataPort=[{
        data_ext :'1',
        data_desc:'1:端口1',
        },{
        data_ext :'2',
        data_desc:'2:端口2',
        }];

    /**/
    var domObj = {
        buttons : {
        	$query_btn : $("#query_btn"),
            $mount_btn : $("#mount_btn"),
            $unmount_btn : $("#unmount_btn"),
            $active_btn : $("#active_btn"),
        },
        $bayIdSel : $("#bayIdSel"),
        $opeIdSel : $("#opeIdSel"),
        $toolIdSel : $("#toolIdSel"),
        $woIdSel : $("#woIdSel"),
//        $mtrlCateSel : $("#mtrlCateSel"),
        $mtrlProdIdTxt : $("#mtrlProdIdTxt"),
        $mtrlLotIdTxt : $("#mtrlLotIdTxt"),
        $mtrlQtyTxt : $("#mtrlQtyTxt"),
        $mtrlMountPort : $("#mtrlMountPort"),

        grids : {
            $mtrlMountListDiv : $("#mtrlMountListDiv"),
            $mtrlMountListGrd : $("#mtrlMountListGrd"),
            $mtrlMountListPg : $("#mtrlMountListPg"),
            $mtrlMountCheckListDiv : $("#mtrlMountCheckListDiv"),
            $mtrlMountCheckListGrd : $("#mtrlMountCheckListGrd"),
            $mtrlMountCheckListPg : $("#mtrlMountCheckListPg")
        }
    };

    var controlsFunc = {
	    //表格自适应
		resizeFnc : function(){
	        var offsetBottom, divWidth,offsetBottom1, divWidth1;
	        divWidth = $("#mtrlMountListDiv").width();
	        offsetBottom =  $(window).height() - $("#mtrlMountListDiv").offset().top;
	        $("#mtrlMountListDiv").height(offsetBottom * 0.95);
	        $("#mtrlMountListGrd").setGridWidth(divWidth * 0.99);
	        $("#mtrlMountListGrd").setGridHeight(offsetBottom * 0.99 - 101);
	        //2
	        divWidth1= $("#mtrlMountCheckListDiv").width();
	        offsetBottom1 =  $(window).height() - $("#mtrlMountCheckListDiv").offset().top;
	        $("#mtrlMountCheckListDiv").height(offsetBottom1 * 0.95);
	        $("#mtrlMountCheckListGrd").setGridWidth(divWidth1 * 0.99);
	        $("#mtrlMountCheckListGrd").setGridHeight(offsetBottom1 * 0.99 - 101);

	    },
    	iniBayInfo : function(){
            comAddValueByDataCateFnc("#bayIdSel", "AREA", "data_ext", "data_desc", true);
    	},
    	iniOpeInfo : function(){
            var inObj = {
                trx_id : VAL.T_FBPBISOPE,
                action_flg : "L"
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oaryA = outObj.oary;
                oaryA.sort(compare);
                var options = [];
                domObj.$opeIdSel.append("<option ></option>");
                for(i=0;i<outObj.oary.length;i++){
                    options[i] = "<option value="+ oaryA[i]["ope_id"] 
                    	+ "@" + oaryA[i]["ope_ver"] 
                    	+ "@" + oaryA[i]["proc_id"] 
                    	+ "@" + oaryA[i]["toolg_id"] 
                    	+ ">" + oaryA[i]["ope_id"] 
                    	+ ":" + oaryA[i]["ope_dsc"] +"</option>";
                }
                domObj.$opeIdSel.append(options.join(""));
                domObj.$opeIdSel.select2({
                    theme : "bootstrap"
                });
            }
    	},
    	iniToolInfo : function(){
            var bayId  = $.trim(domObj.$bayIdSel.val());
            var opeIdInfo= $.trim(domObj.$opeIdSel.val()).split("@");
            if(!bayId){
            	return false;
            }
            var toolGroupId = opeIdInfo[3];
            if(!toolGroupId){
            	return false;
            }
            var inObj = {
                trx_id : VAL.T_FBPBISTOL,
                action_flg : "Q",
                iary : [{
                    bay_id : bayId,
                    tool_group_id_fk : toolGroupId
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr_(domObj.$toolIdSel, outObj.oary, "tool_id","tool_id","tool_dsc",true);
            }
    	},
    	iniWoInfo : function(){
            var bayId  = $.trim(domObj.$bayIdSel.val());
            var opeIdInfo= $.trim(domObj.$opeIdSel.val()).split("@");
            if(!bayId){
            	return false;
            }
            var opeId = opeIdInfo[0];
            if(!opeId){
            	return false;
            }
            var inObj = {
                trx_id : VAL.T_MSPLINEWO,
                action_flg : "Q",
                line_id : bayId,
                ope_id : opeId,
                valid_flg : "Y"
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$woIdSel, outObj.oary, "wo_id","wo_id",true, true);
            }
    	},
//    	iniMtrlCate : function(){
//    		domObj.$mtrlCateSel.val("").trigger("change");
//    		domObj.$mtrlCateSel.empty();
//            var opeIdInfo= $.trim(domObj.$opeIdSel.val()).split("@");
//            var opeId = opeIdInfo[0];
//            
//            if(!opeId){
//            	return false;
//            }
//            var inObj = {
//                trx_id : VAL.T_FBPBISDAT,
//                action_flg : "Q",
//                iary : [{
//                	data_cate : "MTCT",
//                	ext_2 : opeId,
//                	ext_3 : "Y"
//                }]
//            };
//            var outObj = comTrxSubSendPostJson(inObj);
//            if (outObj.rtn_code == VAL.NORMAL) {
//                SelectDom.addSelectArr(domObj.$mtrlCateSel, outObj.oary, "data_ext","data_desc",true, true);
//            }
//    	},
        iniMtrlPort : function(){
            SelectDom.addSelectArr(domObj.$mtrlMountPort, dataPort, "data_ext","data_desc",true, true);
        }
    };

    var btnFunc = {
        queryFunc : function(){
        	var bayId = $.trim(domObj.$bayIdSel.val());
        	var opeIdFk = $.trim(domObj.$opeIdSel.val()).split("@")[0];
        	var toolIdFk = $.trim(domObj.$toolIdSel.val());
        	var woIdFk = $.trim(domObj.$woIdSel.val());
        	if(!bayId){
        		showErrorDialog("", "请选择线别代码");
        		return false;
        	}
        	if(!opeIdFk){
        		showErrorDialog("", "请选择站点代码");
        		return false;
        	}
        	if(!toolIdFk){
        		showErrorDialog("", "请选择设备代码");
        		return false;
        	}
        	if(!woIdFk){
        		showErrorDialog("", "请选择工单代码");
        		return false;
        	}
        	var iary= {
        			line_id  : bayId ,
        			ope_id   : opeIdFk,
        			tool_id  : toolIdFk,
        			wo_id    : woIdFk
        	}
        	var inObj = {
        			trx_id : VAL.T_MTPTOOLMU,
        			action_flg : "Q",
        			iary : [iary]
        	};
        	var outObj = comTrxSubSendPostJson(inObj);
        	if (outObj.rtn_code == VAL.NORMAL) {
        		setGridInfo(outObj.oaryA, "#mtrlMountListGrd");
        		setGridInfo(outObj.oaryB, "#mtrlMountCheckListGrd");
        	}
        },
    	mountFunc : function(){
    		var lineId = $.trim(domObj.$bayIdSel.val());
    		var opeIdFk = ($.trim(domObj.$opeIdSel.val())).split("@");
    		var opeId = opeIdFk[0];
    		var toolId = $.trim(domObj.$toolIdSel.val());
    		var woId = $.trim(domObj.$woIdSel.val());
//    		var mtrlCate = $.trim(domObj.$mtrlCateSel.val());
    		var mtrlProdId = $.trim(domObj.$mtrlProdIdTxt.val());
    		var mtrlLotId = $.trim(domObj.$mtrlLotIdTxt.val());
    		var mtrlQty = $.trim(domObj.$mtrlQtyTxt.val());
    		var mtrlPort = $.trim(domObj.$mtrlMountPort.val());

    		if(!lineId){
                showErrorDialog("", "请选择线别代码");
                return false;
    		}
    		if(!opeId){
                showErrorDialog("", "请选择站点代码");
                return false;
    		}
    		if(!toolId){
                showErrorDialog("", "请选择设备代码");
                return false;
    		}
    		if(!woId){
                showErrorDialog("", "请选择工单代码");
                return false;
    		}
//    		if(!mtrlCate){
//                showErrorDialog("", "请选择物料种类");
//                return false;
//    		}
            if(!mtrlPort){
                showErrorDialog("", "请选择上料端口");
                return false;
            }
    		if(!mtrlProdId){
                showErrorDialog("", "请输入物料代码");
                return false;
    		}
    		if(!mtrlQty){
                showErrorDialog("", "请输入物料数量");
                return false;
    		}
            var iary= {
                line_id : lineId,
                ope_id : opeId,
                tool_id : toolId,
                wo_id : woId,
//                mtrl_cate : mtrlCate,
                mtrl_port : mtrlPort,
                mtrl_prod_id : mtrlProdId,
                mtrl_lot_id : mtrlLotId,
                mtrl_qty : mtrlQty
            }
            var inObj = {
                trx_id : VAL.T_MTPTOOLMU,
                action_flg : "A",
                opi_ope_flg : 'Y',
                evt_usr : VAL.EVT_USER,
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                btnFunc.queryFunc();
                showSuccessDialog("物料装配成功");
            }
    	},
    	unmountFunc : function(){
            var lineId = $.trim(domObj.$bayIdSel.val());
            var opeIdFk = ($.trim(domObj.$opeIdSel.val())).split("@");
            var opeId = opeIdFk[0];
            var toolId = $.trim(domObj.$toolIdSel.val());
            var woId = $.trim(domObj.$woIdSel.val());
//            var mtrlCate = $.trim(domObj.$mtrlCateSel.val());
            var mtrlProdId = $.trim(domObj.$mtrlProdIdTxt.val());
            var mtrlLotId = $.trim(domObj.$mtrlLotIdTxt.val());
            var mtrlQty = $.trim(domObj.$mtrlQtyTxt.val());
            var mtrlPort = $.trim(domObj.$mtrlMountPort.val());

            if(!lineId){
                showErrorDialog("", "请选择线别代码");
                return false;
            }
            if(!opeId){
                showErrorDialog("", "请选择站点代码");
                return false;
            }
            if(!toolId){
                showErrorDialog("", "请选择设备代码");
                return false;
            }
            if(!woId){
                showErrorDialog("", "请选择工单代码");
                return false;
            }
//            if(!mtrlCate){
//                showErrorDialog("", "请选择物料种类");
//                return false;
//            }
            if(!mtrlPort){
                showErrorDialog("", "请选择上料端口");
                return false;
            }
            if(!mtrlQty){
                showErrorDialog("", "请输入物料数量");
                return false;
            }
            var iary= {
                line_id : lineId,
                ope_id : opeId,
                tool_id : toolId,
                wo_id : woId,
//                mtrl_cate : mtrlCate,
                mtrl_port : mtrlPort,
                mtrl_prod_id : mtrlProdId,
                mtrl_lot_id : mtrlLotId,
                mtrl_qty : mtrlQty
            }
            var inObj = {
                trx_id : VAL.T_MTPTOOLMU,
                action_flg : "D",
                opi_ope_flg : 'Y',
                evt_usr : VAL.EVT_USER,
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                btnFunc.queryFunc();
                showSuccessDialog("物料解除装配成功");
            }
    	},
        activeFunc : function(){
        	var selectId = domObj.grids.$mtrlMountListGrd.jqGrid('getGridParam','selrow');
        	if(!selectId){
                showErrorDialog("", "请选择需要激活/关闭的物料装配信息");
                return false;
        	}

        	var rowData = domObj.grids.$mtrlMountListGrd.jqGrid("getRowData",selectId);
            var iary= {
                tool_id : rowData["tool_id_fk"],
                mtrl_cate : rowData["mtrl_cate"],
                mtrl_prod_id : rowData["mtrl_prod_id_fk"],
                wo_id : rowData["wo_id_fk"],
                mtrl_port : rowData["mount_loca"]
            }
            var inObj = {
                trx_id : VAL.T_MTPTOOLMU,
                action_flg : "T",
                evt_usr : VAL.EVT_USER,
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                btnFunc.queryFunc();
                if("Y" == rowData["mtrl_mode"]){
                    showSuccessDialog("物料装配信息关闭成功");
                }else{
                    showSuccessDialog("物料装配信息激活成功");
                }
            }
        },
    };
    
    var iniGridInfo = function(){
    	domObj.grids.$mtrlMountListGrd.jqGrid({
	        url: "",
	        datatype: "local",
	        mtype: "POST",
	        autowidth: true,
            shrinkToFit:false,
            scroll: false,
	        resizable: true,
	        loadonce: true,
	        viewrecords: true,
	        rownumbers: true,
	        emptyrecords: true,
	        colModel: [
                {
                    name: 'mtrl_prod_id_fk',
                    index: 'mtrl_prod_id_fk',
                    label: MTRL_ID_TAG,
                    width: 100
                }, {
	                name: 'mtrl_lot_id',
	                index: 'mtrl_lot_id',
	                label: MTRL_LOT_ID_TAG,
	                width: 100
	            }, {
	                name: 'mtrl_cate',
	                index: 'mtrl_cate',
	                label: MTRL_CATE_TAG,
	                width: 60
	            }, {
                    name: 'mount_loca',
                    index: 'mount_loca',
                    label: MOUNT_PORT,
                    width: 60
                },{
                    name: 'mtrl_stat',
                    index: 'mtrl_stat',
                    label: STATUS_TAG,
                    width: 50
                },{
                    name: 'mtrl_mode',
                    index: 'mtrl_mode',
                    label: '激活标记',
                    width: 60
                },{
	                name: 'nuse_qty',
	                index: 'nuse_qty',
	                label: '可用数量',
	                width: 80
	            }, {
	                name:  'mount_qty',
	                index: 'mount_qty',
	                label: MOUNT_QTY,
	                width: 80
	            },{
	                name: 'use_qty',
	                index: 'use_qty',
	                label: USE_QTY_TAG,
	                width: 80
	            },{
                    name: 'tool_id_fk',
                    index: 'tool_id_fk',
                    label: TOOL_ID_TAG,
                    width: 100
                },{
                    name: 'wo_id_fk',
                    index: 'wo_id_fk',
                    label: WO_ID_TAG,
                    width: 100
                },{
                    name: 'line_id_fk',
                    index: 'line_id_fk',
                    label: LINE_ID_TAG,
                    width: 100
                }, {
                    name: 'wo_qty',
                    index: 'wo_qty',
                    label: WO_QTY,
                    width: 100
                },{
                    name: 'ope_id_fk',
                    index: 'ope_id_fk',
                    label: OPE_ID_TAG,
                    width: 100
                }],
	        pager: "#mtrlMountListPg"
	    });
    	
    	domObj.grids.$mtrlMountCheckListGrd.jqGrid({
	        url: "",
	        datatype: "local",
	        mtype: "POST",
	        autowidth: true,
            shrinkToFit:false,
            scroll: false,
	        resizable: true,
	        loadonce: true,
	        viewrecords: true,
	        rownumbers: true,
	        emptyrecords: true,
	        colModel: [
	            {
	                name:  'mtrl_cate',
	                index: 'mtrl_cate',
	                label: MTRL_CATE_TAG,
	                width: 150
	            }, {
	                name:  'mtrl_stat',
	                index: 'mtrl_stat',
	                label: '检查结果',
	                width: 150
	            }],
	        pager: "#mtrlMountCheckListPg"
	    });
    };
    
    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
    	controlsFunc.iniBayInfo();
    	controlsFunc.iniOpeInfo();
        controlsFunc.iniMtrlPort();
    };
    
    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
    	domObj.buttons.$query_btn.click(function(){
    		btnFunc.queryFunc();
    	});
    	
    	domObj.buttons.$mount_btn.click(function(){
    		btnFunc.mountFunc();
    	});
    	
    	domObj.buttons.$unmount_btn.click(function(){
    		btnFunc.unmountFunc();
    	});
    	
        domObj.buttons.$active_btn.click(function(){
            btnFunc.activeFunc();
        });
    };
    
    /**
     * Other action bind
     */
    var otherActionBind = function(){
	    $("form").submit(function(){
	        return false;
	    });
	    
	    $(window).resize(function() {
	    	controlsFunc.resizeFnc();
	    });
	    
	    domObj.$bayIdSel.change(function(){
        	domObj.grids.$mtrlMountListGrd.jqGrid("clearGridData");
        	domObj.grids.$mtrlMountCheckListGrd.jqGrid("clearGridData");
	    	SelectDom.initWithSpace(domObj.$toolIdSel);
	    	SelectDom.initWithSpace(domObj.$woIdSel);
	    	// TODO $(smpGrid).jqGrid("clearGridData");
	    	controlsFunc.iniToolInfo();
	    	controlsFunc.iniWoInfo();
	    });
	    
	    domObj.$opeIdSel.change(function(){
        	domObj.grids.$mtrlMountListGrd.jqGrid("clearGridData");
        	domObj.grids.$mtrlMountCheckListGrd.jqGrid("clearGridData");
	    	SelectDom.initWithSpace(domObj.$toolIdSel);
	    	SelectDom.initWithSpace(domObj.$woIdSel);
//	    	SelectDom.initWithSpace(domObj.$mtrlCateSel);
	    	// TODO $(smpGrid).jqGrid("clearGridData");
	    	controlsFunc.iniToolInfo();
	    	controlsFunc.iniWoInfo();
//	    	controlsFunc.iniMtrlCate();
	    });
	    
	    domObj.$toolIdSel.change(function(){
        	domObj.grids.$mtrlMountListGrd.jqGrid("clearGridData");
        	domObj.grids.$mtrlMountCheckListGrd.jqGrid("clearGridData");
	    });
	    
	    domObj.$woIdSel.change(function(){
        	domObj.grids.$mtrlMountListGrd.jqGrid("clearGridData");
        	domObj.grids.$mtrlMountCheckListGrd.jqGrid("clearGridData");
	    });
    };

    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
    	iniGridInfo();
        controlsFunc.resizeFnc();
    	iniContorlData();
    	iniButtonAction();
    	otherActionBind();
    };
    
    initializationFunc();

});
